package com.lantosec.hmrp.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.monitor.entity.SupplierTotalEntity;
import com.lantosec.hmrp.modules.monitor.mapper.MonitorSupplierMapper;
import com.lantosec.hmrp.modules.monitor.service.IMonitorSupplierService;
import com.lantosec.hmrp.modules.supplier.entity.HisDrugDir;
import com.lantosec.hmrp.modules.supplier.entity.SupplierAccessory;
import com.lantosec.hmrp.modules.supplier.entity.SupplierInfo;
import com.lantosec.hmrp.modules.supplier.service.ISupplierInfoService;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service()
@Primary
@Log4j2
@SuppressWarnings("all")
public class MonitorSupplierServiceImpl extends ServiceImpl<MonitorSupplierMapper, SupplierTotalEntity> implements IMonitorSupplierService {

    @Autowired
    private ISupplierInfoService supplierInfoService;

    @Autowired
    private ISysDictionariesService dictionariesService;

    @Autowired
    private ISysUploadsService uploadsService;

    @Override
    public List<SupplierTotalEntity> getSupplierDirTotal() {
        return baseMapper.getSupplierDirTotal();
    }

    @Override
    public Page<SupplierTotalEntity> getSupplierDetails(PageVO<SupplierTotalEntity> page, String dictionariesId) {
        PageVO<SupplierTotalEntity> param = null;
        //药品
        if (dictionariesId.equals("001003004")) {
            param = baseMapper.getYPSupplierDetails(page, dictionariesId);
        } else {
            param = baseMapper.getSupplierDetails(page, dictionariesId);
        }
        return param;
    }

    @Override
    public List<SupplierTotalEntity> getSupplierTotal() {
        return baseMapper.getSupplierTotal();
    }

    @Override
    public Page<SupplierTotalEntity> getSupplierName(PageVO<SupplierTotalEntity> page, String dictionariesId) {
        return baseMapper.getSupplierName(page, dictionariesId);
    }

    @Override
    public SupplierInfo getSupplierBaseInfo(String supplierCode) {
        SupplierInfo info = supplierInfoService.getSupplierBaseInfo(supplierCode);
        String supplierType = info.getSupplierType();
        if (StringUtils.isNotEmpty(supplierType)) {
            String supplierTypeName = dictionariesService.getById(supplierType).getDictionariesShowName();
            info.setSupplierTypeName(supplierTypeName);
        }
        String supplierNature = info.getSupplierNature();
        if (StringUtils.isNotEmpty(supplierNature)) {
            String supplierNatureName = dictionariesService.getById(supplierNature).getDictionariesShowName();
            info.setSupplierNatureName(supplierNatureName);
        }
        String supplierIndustry = info.getSupplierIndustry();
        if (StringUtils.isNotEmpty(supplierIndustry)) {
            String supplierIndustryName = dictionariesService.getById(supplierIndustry).getDictionariesShowName();
            info.setSupplierNatureName(supplierIndustryName);
        }
        String supplierScale = info.getSupplierScale();
        if (StringUtils.isNotEmpty(supplierScale)) {
            String supplierScaleName = dictionariesService.getById(supplierScale).getDictionariesShowName();
            info.setSupplierScaleName(supplierScaleName);
        }
        return info;
    }

    @Override
    public Page<HisDrugDir> getSupplierProductInfo(PageVO<SupplierTotalEntity> page, String supplierCode, Integer drugFlag) {
        PageVO<HisDrugDir> param = null;
        if(1 == drugFlag){
            param = baseMapper.getDrugProductInfo(page,supplierCode);
        }else{
            param = baseMapper.getSupplierProductInfo(page,supplierCode);
        }
        return param;
    }

    /***
     * 供应商资质信息
     */
    @Override
    public List<SupplierAccessory>  getSupplierQualificationInfo(String supplierCode) {
        List<SupplierAccessory> supplierAccessory = baseMapper.getSupplierQualificationInfo(supplierCode);
        supplierAccessory.forEach(it->{
            String uploadId = it.getUploadId();
            if(StringUtils.isNotEmpty(uploadId)){
                String url = uploadsService.queryPictureUrlById(uploadId);
                it.setSavePath(url);
            }
        });
        return supplierAccessory;
    }
}
